package com.swordxu.yim.android.imlib.network.packet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.swordxu.yim.android.config.ProtocolConstant;
import com.swordxu.yim.android.entity.User;
import com.swordxu.yim.android.imlib.network.packet.base.DefaultHeader;
import com.swordxu.yim.android.imlib.network.packet.base.Header;
import com.swordxu.yim.android.imlib.network.packet.base.Packet;

/**
 * Created by 徐晓明 on 2015/1/8.
 */
public class LoginPacket extends Packet {

    public LoginPacket(PacketRequest.Entity entity) {
        mRequest = new PacketRequest(entity);
        setNeedMonitor(true);
    }

    public LoginPacket() {

    }

    /**
     * 登录服务器数据包封包
     * @return
     */
    @Override
    public String encode() {
        Header header = mRequest.getHeader();
        PacketRequest req = (PacketRequest) mRequest;
        JSONObject jo = new JSONObject();
        jo.put("header", header);
        jo.put("body", JSON.toJSON(req.entity));

        return jo.toJSONString();
    }

    /**
     * Loginserver数据包解包
     */
    @Override
    public void decode(String buffer) {
        PacketResponse res = new PacketResponse();
        Header header = new Header();
        JSONObject jo = JSON.parseObject(buffer);
        header = jo.getObject("header", Header.class);
        res.setHeader(header);
        res.entity = JSON.parseObject(jo.getString("body"), res.entity.getClass());
        this.mResponse = res;
    }

    /**
     * 请求数据包
     */
    public static class PacketRequest extends Request {
        public static class Entity{
            public String name;
            public String pwd;
            public int onlineStatus;
            public int clientType;
            public String clientVersion;
        }
        public Entity entity;

        public PacketRequest(Entity entity) {
            this.entity = entity;
            Header header = new DefaultHeader(ProtocolConstant.SID_LOGIN, ProtocolConstant.CID_LOGIN_REQ_USERLOGIN);
            setHeader(header);
        }
    }

    /**
     * 响应数据包
     */
    public static class PacketResponse extends Response {
        public static class Entity{
            public int serverTime;
            public int result;
            public int onlineStatus;
            public String name;
            public String userId;
            public String nickName;
            public String avatarUrl;
            public String tile;
            public String position;
            public int roleStatus;
            public int sex;
            public String departId;
            public int jobNUm;
            public String telphone;
            public String email;
            public String token;
            public int userType;
        }

        public User getUser() {
            User user = new User();
            user.setOnlineStatus(entity.onlineStatus);
            user.setUserId(entity.userId);
            user.setNickName(entity.nickName);
            user.setAvatarUrl(entity.avatarUrl);
            user.setTitle("");
            user.setPosition("");
            user.setRoleStatus(0);
            user.setSex(0);
            user.setDepartId(0);
            user.setJobNum(0);
            user.setTelphone("");
            user.setEmail("");
            user.setToken("");

            return user;
        }

        public Entity entity = new Entity();
    }
}
